package cn.fl.comm.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.cache.api.ICacheRefreshApiService;
import cmm.comm.facade.flow.condition.SysFlowRejRsnQueryCondition;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.condition.CacheRefreshCdMapQC;
import cmm.mid.core.framework.condition.CacheRefreshCtrlCfgQC;
import cmm.mid.core.framework.condition.CacheRefreshDictionaryQC;
import cmm.mid.core.framework.condition.CacheRefreshRegionQC;
import cmm.mid.core.framework.condition.CacheRefreshStdRegionQC;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.comm.service.ICacheRefreshService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysTipConstant;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.transform.SysDictionaryVOTransform;

/**
 * 刷新缓存实现接口
 * @author FUJINGRUN
 */
@Service
public class CacheRefreshServiceImpl implements ICacheRefreshService {

	private static final Logger logger = LoggerFactory.getLogger(CacheRefreshServiceImpl.class);
	
	/**
	 * 刷新缓存API
	 */
	@Autowired
	private ICacheRefreshApiService cacheRefreshApiService;
	
	/**
	 * 数据字典服务API
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	/**
	 * SysUsr 服务层BEAN
	 */
	@Autowired
	private ISysUsrService sysUsrService;

	@Override
	public DataResultDTO refreshDictionary(CacheRefreshDictionaryQC qc) throws BaseException {
		DataResultDTO result = new DataResultDTO(String.valueOf(BaseConstant.IS_YESNO_YES),PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_SUCC));
		try {
			result = this.cacheRefreshApiService.refreshDictionary(qc);
		}catch (BaseException e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.info(logger, "refreshDictionary", "刷新数据字典失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.error(logger, "refreshDictionary", "刷新数据字典失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	@Override
	public DataResultDTO refreshCtrlCfg(CacheRefreshCtrlCfgQC qc) throws BaseException {
		DataResultDTO result = new DataResultDTO(String.valueOf(BaseConstant.IS_YESNO_YES),PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_SUCC));
		try {
			result = this.cacheRefreshApiService.refreshCtrlCfg(qc);
		}catch (BaseException e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.error(logger, "refreshCtrlCfg", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.info(logger, "refreshCtrlCfg", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	@Override
	public DataResultDTO refreshCdMap(CacheRefreshCdMapQC qc) throws BaseException {
		DataResultDTO result = new DataResultDTO(String.valueOf(BaseConstant.IS_YESNO_YES),PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_SUCC));
		try {
			result = this.cacheRefreshApiService.refreshCdMap(qc);
		}catch (BaseException e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.error(logger, "refreshCdMap", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.info(logger, "refreshCdMap", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	@Override
	public DataResultDTO refreshRegion(CacheRefreshRegionQC qc) throws BaseException {
		DataResultDTO result = new DataResultDTO(String.valueOf(BaseConstant.IS_YESNO_YES),PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_SUCC));
		try {
			result = this.cacheRefreshApiService.refreshRegion(qc);
		}catch (BaseException e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.error(logger, "refreshRegion", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.info(logger, "refreshRegion", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	@Override
	public DataResultDTO refreshStdRegion(CacheRefreshStdRegionQC qc) throws BaseException {
		DataResultDTO result = new DataResultDTO(String.valueOf(BaseConstant.IS_YESNO_YES),PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_SUCC));
		try {
			result = this.cacheRefreshApiService.refreshStdRegion(qc);
		}catch (BaseException e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.error(logger, "refreshStdRegion", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.info(logger, "refreshStdRegion", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Pager searchGroupListPage(Pager page) throws BaseException {
		page = this.sysDictionaryApiService.searchGroupListPage(page);
		if(null != page && CollectionUtils.isNotEmpty(page.getResultList())){
			List<SysDictionaryDTO> resultList = (List<SysDictionaryDTO>)page.getResultList();
			page.setResultList(SysDictionaryVOTransform.toVOList(resultList));
		}
		return page;
	}

	@Override
	public List<KeyValueVO> searchServiceSysCdList() throws BaseException {
		return this.sysUsrService.searchServiceSysCdList();
	}

	@Override
	public DataResultDTO refreshFlowRejRsn(SysFlowRejRsnQueryCondition qc) throws BaseException {
		DataResultDTO result = new DataResultDTO(String.valueOf(BaseConstant.IS_YESNO_YES),PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_SUCC));
		try {
			result = this.cacheRefreshApiService.refreshFlowRejRsn(qc);
		}catch (BaseException e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.error(logger, "refreshFlowRejRsn", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception e) {
			result.setCode(String.valueOf(BaseConstant.IS_YESNO_NO));
			result.setInfo(PropertiesCacheUtils.getProperty(SysTipConstant.TIP_CMM_REFRESH_FAIL) + e.getMessage());
			BaseLogUtils.info(logger, "refreshFlowRejRsn", "刷新失败!" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 查询数据字典详情-根据系统代码、代码组、代码组名称分组
	 * @param page
	 * @return
	 * @throws BaseException
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Pager searchDicListInfoPage(Pager page) throws BaseException {
		page = this.sysDictionaryApiService.searchDicListInfoPage(page);
		if(null != page && CollectionUtils.isNotEmpty(page.getResultList())){
			List<SysDictionaryDTO> resultList = (List<SysDictionaryDTO>)page.getResultList();
			page.setResultList(SysDictionaryVOTransform.toVOList(resultList));
		}
		return page;
	}
}
